<?php

/**
 * Description of
 *
 * @author yuanjun.liu
 */
class Role extends App {

    public $pageSize = 25;

    protected function getTableName() {
        return 'role';
    }

    protected function getPrimaryField() {
        return 'id';
    }

    public function showAdd() {
        include $this->tpl('roleAdd');
    }

    public function doAdd() {
        if (!$name = (string) trim($_REQUEST['name']))
            $this->redirect('javascript:history.back(-1);', '角色名称必填！', 2, 0);
        $permissions = array_filter((array) $_REQUEST['permissions']);
        if (empty($permissions))
            $this->redirect('javascript:history.back(-1);', '没有选择任何权限！', 2, 0);
        $data = array(
            'name' => $name,
            'order' => (int) $_REQUEST['order'],
            'permissions' => serialize($permissions)
        );
        if ($this->table()->insert($data)) {
            $id = $this->db->getLastInsID();
            if (Permission::update('r', $id, $permissions))
                $this->redirect(jaUrl('role', 'list'), '角色添加成功！正在跳回列表页..', 1, 2);
        }
        $this->redirect(jaUrl('role', 'list'), '角色添加失败！错误信息如下:<br />' . $this->db->getError(), 2, 0);
    }

    public function showEdit() {
        $id = (string) $_REQUEST['id'];
        ($detail = $this->find($id)) || $this->redirect(jaUrl('role', 'list'), '找不到该项！', 2, 0);
        $permissions = Permission::get('r', $id);
        include $this->tpl('roleEdit');
    }

    public function doEdit() {
        if (!$id = (int) trim($_REQUEST['id']))
            $this->redirect(jaUrl('role', 'list'), '没有指定ID！', 2, 0);
        if (!$name = (string) trim($_REQUEST['name']))
            $this->redirect('javascript:history.back(-1);', '角色名称必填！', 2, 0);
        $permissions = array_filter((array) $_REQUEST['permissions']);
        if (empty($permissions))
            $this->redirect('javascript:history.back(-1);', '没有选择任何权限！', 2, 0);
        $data = array(
            'name' => $name,
            'order' => (int) $_REQUEST['order'],
            'permissions' => serialize($permissions)
        );
        if ($this->table()->where(array($this->getPrimaryField(), 'eq', $id))->update($data)) {
            if (Permission::update('r', $id, $permissions))
                $this->redirect(jaUrl('role', 'list'), '更新成功！正在跳回列表页..', 1, 2);
        }
        $this->redirect(jaUrl('role', 'list'), '操作失败！错误信息如下:<br />' . $this->db->getError(), 2, 0);
    }

    public function doDelete() {
        ($id = (string) trim($_REQUEST['id'])) || $this->redirect(jaUrl('role', 'list'), '没有指定ID！', 2, 0);
        if ($this->delete($id)) {
            Permission::delete('r', $id);
            $this->redirect(jaUrl('role', 'list'), '删除成功！正在跳回列表页..', 1, 2);
        } else {
            $this->redirect(jaUrl('role', 'list'), '操作失败！错误信息如下:<br />' . $this->db->getError(), 2, 0);
        }
    }

    public function showList() {
        (isset($_REQUEST['page']) && $page = (int) $_REQUEST['page']) || ($page = 1);
        $total = $this->count();
        $list = $this->table()->limit(($page - 1) * $this->pageSize . ',' . $this->pageSize)->order('`order` ASC')->select();
        if (!empty($list))
            self::processList($list);
        $showPages = showPages($total, $this->pageSize);
        include $this->tpl('roleList');
    }

    static protected function processList(&$list) {
        foreach ($list as &$val) {
            $val['permissions'] = unserialize($val['permissions']);
            $val['permission_names'] = array();
            if (!empty($val['permissions'])) {
                foreach ($val['permissions'] as $pval) {
                    $val['permission_names'][] = $GLOBALS['JA_PERMISSION_CONFIG'][$pval]['name'];
                }
            }
        }
    }
    
    static public function getAll() {
        $list = Db::I()->table('role')->order('`order` ASC')->select();
        if (!empty($list))
            self::processList($list);
        return $list;
    }

}

?>
